//
//  MWMessageController.m
//  XiRan-iOS
//
//  Created by Richfit on 2019/10/30.
//  Copyright © 2019 Wood. All rights reserved.
//

#import "MWMessageController.h"
#import <WebKit/WebKit.h>

@interface MWMessageController () <WKUIDelegate, WKNavigationDelegate>

@property(nonatomic, strong) UIProgressView *progressView;
@property(nonatomic, strong) WKWebView *webView;

@end

@implementation MWMessageController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = [[MWConfigManager manager] objectForKey:@"message_list" defaultValue:@"消息列表"];
    
    [self setupPageSubviews];
    if ([MWConfigManager manager].isNetError) {
        self.errorTitleLabel.text = [[MWConfigManager manager] objectForKey:@"click_retry" defaultValue:@"点击重试"];
        self.errorTitleLabel.hidden = self.errorImageView.hidden = NO;
        self.webView.hidden = YES;
    } else {
        NSString *url = [NSString stringWithFormat:@"%@/addons/kefu/index/mobile", kBaseUr];
        [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
    }
}

- (void)refresh {
    [super refresh];
    if ([MWConfigManager manager].isNetError) {
        self.errorTitleLabel.hidden = self.errorImageView.hidden = NO;
        self.webView.hidden = YES;
    } else {
        self.errorTitleLabel.hidden = self.errorImageView.hidden = YES;
        self.webView.hidden = NO;
        NSString *url = [NSString stringWithFormat:@"%@/addons/kefu/index/mobile", kBaseUr];
        [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];

    }
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
    if ([keyPath isEqual:@"estimatedProgress"] && object == self.webView) {
        [self.progressView setAlpha:1.0f];
        [self.progressView setProgress:self.webView.estimatedProgress animated:YES];
        if (self.webView.estimatedProgress  >= 1.0f) {
            [UIView animateWithDuration:0.3 delay:0.3 options:UIViewAnimationOptionCurveEaseOut animations:^{
                [self.progressView setAlpha:0.0f];
            } completion:^(BOOL finished) {
                [self.progressView setProgress:0.0f animated:YES];
            }];
        }
    }else{
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
}

- (void)dealloc {
    [self.webView removeObserver:self forKeyPath:@"estimatedProgress"];
    [self.webView setNavigationDelegate:nil];
    [self.webView setUIDelegate:nil];
}

- (void)setupPageSubviews {
    WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
    WKPreferences *preference = [[WKPreferences alloc]init];
    preference.javaScriptEnabled = YES;
    preference.javaScriptCanOpenWindowsAutomatically = YES;
    config.preferences = preference;
    
    _webView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:config];
    _webView.UIDelegate = self;
    _webView.navigationDelegate = self;
    _webView.allowsBackForwardNavigationGestures = YES;
    [_webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew context:nil];
    [self.view addSubview:self.webView];
    
    _progressView = [[UIProgressView alloc] init];
    _progressView.progressTintColor = [UIColor greenColor];
    [self.view addSubview:_progressView];
    
    [_webView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    [_progressView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view);
        make.height.equalTo(@2);
    }];
}

@end
